package com.qfedu.controller;

import com.qfedu.model.SubItem;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.ISubItemService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/7/3 0:01
 */

@RestController
@RequestMapping("subitem")
public class SubItemController {

    @Autowired
    private ISubItemService subItemService;

    @GetMapping("findAll")
    public List<SubItem> findAll(){

        List<SubItem> subItems = subItemService.findAll();
        return subItems;
    }

    /**
     * @Author Tbh.
     * @Description: 分页查询分享工程
     * @Date 2022/7/3 0:12
     * @Param QueryPageBean
     * @return PageResult
     */
    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPage) {
        PageResult page = subItemService.findPage(queryPage);
        return page;
    }


    /**
     * @Author Tbh.
     * @Description: 添加分项工程
     * @Date 2022/7/3 0:27
     * @Param SubItem
     * @return Result
     */
    @PostMapping("add")
    public Result addSubItem(@RequestBody SubItem subItem) {
        int row = subItemService.addSubItem(subItem);
        if (row > 0) {
            //枚举类名.对象名.get方法 获取到对应的属性值
            //Status.SUCCESS.name();  获取到枚举对象名  SUCCESS
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * @Author Tbh.
     * @Description: 数据回显
     * @Date 2022/7/3 0:29
     * @Param suid
     * @return Result
     */
    @GetMapping("findBySuid")
    public Result findByPid(@RequestParam("suid") String suid) {
        SubItem subItem = subItemService.findBySuid(suid);
        Result result = new Result();
        result.setMessage("fail");
        if (ObjectUtils.isNotEmpty(subItem)) {
            result.setFlag(true);
            result.setData(subItem);
            result.setMessage("success");
        }
        return result;

    }

    /**
     * @Author Tbh.
     * @Description: 修改分项工程
     * @Date 2022/7/3 0:30
     * @Param SubItem
     * @return Result
     */
    @PostMapping("edit")
    public Result edit(@RequestBody SubItem subItem) {
        int row = subItemService.edit(subItem);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }


    /**
     * @Author Tbh.
     * @Description: 逻辑删除
     * @Date 2022/7/3 0:30
     * @Param suid
     * @return Result
     */
    @GetMapping("delete")
    public Result delete(@RequestParam("suid") String suid){
        int row =  subItemService.deleteBySuid(suid);
        if (row>0){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

}
